<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BRequest
 *
 * @author I062261
 */
class BRequest extends BEntityWithGroup {

    public static function findAll($context) {
        //parent::findAll($context);

        $attributes = array();

        //find by from 
        if (isset($context['sender'])) {
            $sender = $context['sender'];

            if (isset($sender['userId']))
                $attributes['to_user'] = $sender['userId'];
            if (isset($sender['spaceId']))
                $attributes['to_Space'] = $sender['spaceId'];
            if (isset($sender['appId']))
                $attributes['to_Space_app'] = $sender['appId'];
            if (isset($sender['operationFlowId']))
                $attributes['to_operation_flow'] = $sender['operationFlowId'];
            if (isset($sender['operationId']))
                $attributes['to_operation'] = $sender['operationId'];
        }

        if (isset($context['receiver'])) {
            $receiver = $context['receiver'];

            if (isset($receiver['userId']))
                $attributes['from_user'] = $receiver['userId'];
            if (isset($receiver['spaceId']))
                $attributes['from_Space'] = $receiver['spaceId'];
            if (isset($receiver['appId']))
                $attributes['from_Space_app'] = $receiver['appId'];
            if (isset($receiver['operationFlowId']))
                $attributes['from_operation_flow'] = $receiver['operationFlowId'];
            if (isset($receiver['operationId']))
                $attributes['from_operation'] = $receiver['operationId'];
        }

        if (isset($context['relationshipId']))
            $attributes['relationship_id'] = $context['relationshipId'];

        //get all not deleted request
        $attributes['status'] = isset($context['status']) ? $context['status'] : 1;

        if (isset($context['startTime']))
            $attributes['start_time'] = $context['startTime'];

        if (isset($context['endTime']))
            $attributes['end_time'] = $context['endTime'];
        
        return $this->retriveFromStore($attributes, $context);
    }

    protected function retriveFromStore($attributes, $context = NULL) {

        if (isset($context['requestStatus']))
            $attributes['request_status'] = $context['requestStatus'];

        if (isset($context['parentRequestId']))
            $attributes['parent_request_id'] = $context['parentRequestId'];

        $sql = 'select * from bln_app_request where ';

        foreach ($attributes as $key => $value) {
            if ($key != 'start_time' || $key != 'end_time' || $key != 'status') {
                $sql = $sql . $key . '=' . $value . ' and ';
            }

            //start_time value should like < '2013-08-12'
            if ($key == 'start_time' || $key == 'end_time')
                $sql = $sql . $key . $value . 'and ';
        }

        $sql = $sql . ' status =' . $attributes['status'];

        $mItems = BMRequest::model()->findAllBySql($sql);

        $requests = array();

        foreach ($mItems as $mItem) {
            $requests[$mItem->_id] = new BRequest($mItem->_id);
        }

        if ($requests != NULL)
            return $requests;

        return FALSE;
    }

}

?>
